package com.ruiysoft.crm.controller;

import cn.taroco.common.utils.Query;
import cn.taroco.common.web.Response;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruiysoft.crm.model.entity.BizWebsite;
import com.ruiysoft.crm.service.BizWebsiteService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * @author fanyl
 */
@Api(tags = "常用网站")
@RestController
@RequestMapping("/website")
public class BizWebsiteController {

    @Autowired
    private BizWebsiteService bizWebsiteService;

    /**
     * 通过ID查询信息
     *
     * @param id ID
     * @return
     */
    @GetMapping("/get/{id}")
    public BizWebsite get(@PathVariable Integer id) {
        return bizWebsiteService.getById(id);
    }

    /**
     * 分页查询
     *
     * @param params 分页对象
     * @return 分页对象
     */
    @GetMapping("/list/page")
    public IPage dictPage(@RequestParam Map<String, Object> params) {
        return bizWebsiteService.page(new Query<>(params), new QueryWrapper<>());
    }

    /**
     * 添加
     *
     * @param bizWebsite
     * @return success、false
     */
    @PostMapping("/insert")
    public Response add(@RequestBody BizWebsite bizWebsite) {
        bizWebsite.setCreateDate(LocalDateTime.now());
        return Response.success(bizWebsiteService.save(bizWebsite));
    }

    /**
     * 删除
     *
     * @param id ID
     * @return R
     */
    @DeleteMapping("/delete/{id}")
    public Response delete(@PathVariable Integer id) {
        return Response.success(bizWebsiteService.removeById(id));
    }

    /**
     * 修改
     *
     * @param bizWebsite
     * @return success/false
     */
    @PutMapping("/update")
    public Response editDict(@RequestBody BizWebsite bizWebsite) {
        bizWebsite.setModificationDate(LocalDateTime.now());
        return Response.success(bizWebsiteService.updateById(bizWebsite));
    }
}
